package com.zyp.test.test;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.BounceInterpolator;

/**
 * @author zyp
 *         2017/12/27
 */

public class PointCircleView extends View {

    private Paint mPaint;
    private MyPoint mMyPoint = new MyPoint(20, 20);

    public PointCircleView(Context context) {
        super(context);
        init();
    }

    public PointCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PointCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas, mMyPoint, 20);
    }

    private void drawCircle(Canvas canvas, MyPoint myPoint, int i) {
        canvas.drawCircle(myPoint.getX(), myPoint.getY(), 20, mPaint);
    }

    public void startAnimation() {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mMyPoint, new MyPoint(getWidth() - 20, getHeight() - 20));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mMyPoint = (MyPoint) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setDuration(5000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        
        valueAnimator.setInterpolator(new BounceInterpolator());
        valueAnimator.start();
    }

}
